﻿using System;
using System.Windows.Forms;
using DataAccess;
using DevExpress.XtraEditors;
using NLog;

namespace QuanLyKhachSan
{
    public partial class FormDichVuCapNhat : XtraForm
    {
        private readonly DichVuManager _dvManager = new DichVuManager();
        protected static Logger _logger = LogManager.GetCurrentClassLogger();

        public FormDichVuCapNhat()
        {
            InitializeComponent();
        }

        public FormDichVuCapNhat(DichVu dvCapNhat)
        {
            InitializeComponent();
            InitForm(dvCapNhat);
        }

        private void InitForm(DichVu dv)
        {
            txtMaDV.Text = dv.MaDV.ToString();
            txtTenDV.Text = dv.TenDichVu;
            chbTrangThai.Checked = dv.TrangThai;
            nbGia.Value = dv.Gia;
            txtDonViTinh.Text = dv.DonViTinh;
        }

        private void btnThoat_Click(object sender, EventArgs e)
        {
            Close();
        }

        private DichVu GetServicesData()
        {
            var dv = new DichVu();
            try
            {
                dv.TenDichVu = txtTenDV.Text;
                dv.Gia =nbGia.Value;
                dv.TrangThai = chbTrangThai.Checked;
                dv.DonViTinh = txtDonViTinh.Text;
                return dv;
            }
            catch (Exception ex)
            {
                _logger.Error(ex);
                XtraMessageBox.Show("Vui lòng điền đầy đủ thông tin ", "Thông báo", MessageBoxButtons.OK,
                    MessageBoxIcon.Warning);
                return null;
            }
        }

        private void btnThaoTac_Click(object sender, EventArgs e)
        {
            var services = GetServicesData();
            if (services != null)
            {
                var validateRs = _dvManager.Validate(services);
                if (validateRs.Valid())
                {
                    ActionResult acResult;
                    if (String.Compare(btnThaoTac.Text, "Add", StringComparison.OrdinalIgnoreCase) != 0)
                    {
                        //Cập nhật
                        services.MaDV = int.Parse(txtMaDV.Text);
                        acResult = _dvManager.InsertOrUpdate(services);
                    }
                    else
                        acResult = _dvManager.Add(services);

                    if (!acResult.Sucess())
                    {
                        _logger.Info(acResult.Exceptions);
                        XtraMessageBox.Show(acResult.GetErrorMessage());
                    }
                    else{
                        DialogResult = DialogResult.OK;
                        Close();
                    }
                }
                else
                {
                    _logger.Error(validateRs.Message);
                    XtraMessageBox.Show(validateRs.ToString(), "Lỗi", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
        }

        private void nbGia_Scroll(object sender, ScrollEventArgs e)
        {
            nbGia.Value += 1;
        }

        private void nbGiamGia_Scroll(object sender, ScrollEventArgs e)
        {
            nbGiamGia.Value += 1;
        }

        private void FormDichVuCapNhat_Activated(object sender, EventArgs e)
        {
            txtTenDV.Focus();
        }
    }
}
